//
//  GenderPickerView.m
//  project
//
//  Created by 圆圆陈 on 2018/1/20.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "GenderPickerView.h"

@interface GenderPickerView()
@property(nonatomic,strong)NSString *returnStr;
@property(nonatomic,strong)UIButton *maleBtn;
@property(nonatomic,strong)UIButton *femaleBtn;
@end

@implementation GenderPickerView

{
    UIView *backView;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        _returnStr = [NSString string];
        
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT*2/3, HHHWIDTH, HHHHEIGHT/3)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(70), FitH(30));
        [cancelBtn setTitleNormal:HHH_Text(@"退出")];
        [cancelBtn setTitleColorNormal:SNCOLORBLACK];
        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(HHHWIDTH-FitW(70), FitH(10), FitW(50), FitH(30));
        [sureBtn setTitleNormal:HHH_Text(@"确定")];
        [sureBtn setTitleColorNormal:SNCOLORBLACK];
        [sureBtn addTouchUpTarget:self action:@selector(sureBtnClick)];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50),HHHWIDTH , 1)];
        line.backgroundColor = SNCOLORGRAYLINE;
        [backView addSubview:line];
        
        _femaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _femaleBtn.frame = CGRectMake(HHHWIDTH/2-FitW(100), line.bottom+FitH(50), FitW(200), FitH(30));
        [_femaleBtn setImageNormal:@"未选中"];
        [_femaleBtn setImageSelecte:@"选中2"];
        [_femaleBtn setTitleNormal:HHH_Text(@"女")];
        [_femaleBtn setTitleColorNormal:SNCOLORBLACK];
        _femaleBtn.titleEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, -FitW(30));
        _femaleBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, -FitW(30), 0, 0);
        [_femaleBtn addTouchUpTarget:self action:@selector(femaleBtnClick:)];
        [backView addSubview:_femaleBtn];
        
        _maleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _maleBtn.frame = CGRectMake(HHHWIDTH/2-FitW(100), _femaleBtn.bottom+FitH(30), FitW(200), FitH(30));
        [_maleBtn setImageNormal:@"未选中"];
        [_maleBtn setImageSelecte:@"选中2"];
        [_maleBtn setTitleNormal:HHH_Text(@"男")];
        [_maleBtn setTitleColorNormal:SNCOLORBLACK];
        _maleBtn.titleEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, -FitW(30));
        _maleBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, -FitW(30), 0, 0);
        [_maleBtn addTouchUpTarget:self action:@selector(maleBtnClick:)];
        [backView addSubview:_maleBtn];
    }
    return self;
}
-(void)setGender:(NSInteger)gender{
     _femaleBtn.selected = gender==1?YES:NO;
     _maleBtn.selected = gender==2?YES:NO;
}
-(void)cancelBtnClick{
    [self animationedDown];
}
-(void)sureBtnClick{
    _returnStr = _femaleBtn.selected?SN_Text(@"女"):SN_Text(@"男");
    if (self.sureButtonBlock) {
        self.sureButtonBlock(_returnStr);
    }
    [self animationedDown];
}
-(void)femaleBtnClick:(UIButton *)sender{
    if (!sender.selected) {
        sender.selected = !sender.selected;
        _maleBtn.selected = NO;
    }
}
-(void)maleBtnClick:(UIButton *)sender{
    if (!sender.selected) {
        sender.selected = !sender.selected;
        _femaleBtn.selected = NO;
    }
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    return YES;
}

@end
